import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.docx4j.convert.in.xhtml.FormattingOption;
import org.docx4j.convert.in.xhtml.XHTMLImporter;
import org.docx4j.convert.in.xhtml.XHTMLImporterImpl;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.packages.SpreadsheetMLPackage;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart;
import org.docx4j.wml.RFonts;

import java.io.File;

/**
 * 描述:
 *
 * @author yanzhengwu
 * @create 2020-12-25 13:45
 */
public class TestHtmlToWord {
   static String stringFromFile="<div id=\"table\">\n" +
            "     <table id=\"targetTable\">\n" +
            "         <thead>\n" +
            "             <tr>\n" +
            "                 <th style=\"background: fuchsia;\">名次</th>\n" +
            "                 <th>姓名</th>\n" +
            "                 <th>成绩</th>\n" +
            "             </tr>\n" +
            "         </thead>\n" +
            "         <tbody>\n" +
            "             <tr align=\"center\">\n" +
            "                 <td>1</td>\n" +
            "                 <td>小明</td>\n" +
            "                 <td>100</td>\n" +
            "                 <td>120</td>\n" +
            "             </tr>\n" +
            "             <tr align=\"center\">\n" +
            "                 <td>2</td>\n" +
            "                 <td>小红</td>\n" +
            "                 <td>95.5</td>\n" +
            "             </tr>\n" +
            "         </tbody>\n" +
            "     </table>\n" +
            " </div>";

    public static void main(String[] args) throws Exception {
        //String inputfilepath = "C:/Users//Administrator/Desktop/test.html";
        String baseURL = "F:htmlToExcel";

        //String stringFromFile = FileUtils.readFileToString(new File(inputfilepath), "UTF-8");

        String unescaped = stringFromFile;
        if (stringFromFile.contains("&lt;/") ) {
            unescaped = StringEscapeUtils.unescapeHtml(stringFromFile);
        }
        // 设置字体映射
        RFonts rfonts = Context.getWmlObjectFactory().createRFonts();
        rfonts.setAscii("Century Gothic");
        XHTMLImporterImpl.addFontMapping("Century Gothic", rfonts);

        // 创建一个空的docx对象
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
        XHTMLImporter importer = new XHTMLImporterImpl(wordMLPackage);
        importer.setTableFormatting(FormattingOption.IGNORE_CLASS);
        importer.setParagraphFormatting(FormattingOption.IGNORE_CLASS);

        NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
        wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
        ndp.unmarshalDefaultNumbering();

        // 转换XHTML，并将其添加到我们制作的空docx中
        XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);

        XHTMLImporter.setHyperlinkStyle("Hyperlink");
        wordMLPackage.getMainDocumentPart().getContent().addAll(
                XHTMLImporter.convert(unescaped, baseURL));
        wordMLPackage.save(new File("F:htmlToExcel/test.xlsx"));
    }
}
